﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace UI.UserControls
{
    public partial class MFATextBox : UserControl
    {
        public MFATextBox()
        {
            InitializeComponent();
            exColor = ((SolidColorBrush)base.Foreground).Color;
            textBox1.LostFocus += new RoutedEventHandler(textBox1_LostFocus);
            textBox1.GotFocus += new RoutedEventHandler(textBox1_GotFocus);
            textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
        }

        void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBox1.Text!=BackGroundText)
            {
                Text = textBox1.Text;
            }            
        }

        void textBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text.Trim() == "")
            {
                textBox1.Text = BackGroundText;
                exColor = ((SolidColorBrush)base.Foreground).Color;
                textBox1.Foreground = new SolidColorBrush(Colors.Gray);
            }
            else
            {
                textBox1.Foreground = new SolidColorBrush(exColor);
            }
        }

        void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text == BackGroundText)
            {
                textBox1.Text = "";
            }
            textBox1.Foreground = new SolidColorBrush(exColor);
        }

        private string _BackGroundText;
        public string BackGroundText
        {
            get { return _BackGroundText; }
            set
            {
                _BackGroundText = value;
                textBox1.Text = value;
                textBox1.Foreground = new SolidColorBrush(Colors.Gray);
            }
        }

        private Color exColor;

        public string Text
        {
            get
            {
                if ((string)GetValue(TextProperty)==BackGroundText)
                {
                    return "";
                }
                return (string)GetValue(TextProperty);
            }
            set
            {
                if (value == Text)
                {
                    return;
                }

                if (value.Trim()=="")
                {
                    textBox1.Text = BackGroundText;
                    SetValue(TextProperty, BackGroundText);
                }
                else
                {
                    SetValue(TextProperty, value);
                    textBox1.Text = value;
                }
            }
        }

        // Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(MFATextBox), new PropertyMetadata(null, onTextChanged));



        public static void onTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
    }
}
